package pwd.allen.tuomin;

import cn.hutool.db.AbstractDb;
import cn.hutool.db.Db;
import cn.hutool.db.ds.DataSourceWrapper;
import cn.hutool.db.ds.pooled.PooledDataSource;
import cn.hutool.json.JSONUtil;

import java.io.IOException;
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * @author 门那粒沙
 * @create 2022-04-04 14:24
 **/
public class Main {
    public static void main(String[] args) throws SQLException, IOException, NoSuchFieldException, IllegalAccessException {
        TuominUtil tuominUtil = new TuominUtil();

        Field field = AbstractDb.class.getDeclaredField("ds");
        field.setAccessible(true);
        DataSourceWrapper obj = (DataSourceWrapper) field.get(tuominUtil.db);
        PooledDataSource dataSource = (PooledDataSource) obj.getRaw();
        dataSource.setLoginTimeout(2);
        System.out.println("------数据库配置------");
        System.out.println(JSONUtil.toJsonStr(dataSource.getConfig()));

        print();

        Scanner scanner = new Scanner(System.in);
        String command = null;
        while (!"q".equals(command = scanner.next())) {
            if ("a".equals(command)) {
                tuominUtil.outputData();
            } else if ("b".equals(command)) {
                tuominUtil.matchRegex();
            }
            print();
        }
    }

    static void print() {
        System.out.println("请输入命令：");
        System.out.println("    a）：读取数据库数据");
        System.out.println("    b）：匹配数据库数据");
        System.out.println("    q）：退出");
    }
}
